package org.example.springboot01.service.impl;


import org.example.springboot01.mapper.Usermapper;
import org.example.springboot01.pojo.User;
import org.example.springboot01.service.Userservice;
import org.example.springboot01.utils.MD5Util;
import org.example.springboot01.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class Userserviceimpl implements Userservice {
    @Override
    public void updateuser(User user) {
         usermapper.updateuser(user);
    }

    @Override
    public void updateavatar(String avatar) {
        //根据用户名插入头像
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        usermapper.updateavatar(avatar,username);
    }

    @Override
    public void updatepassword(String npwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        String md5 = MD5Util.getMD5(npwd);
        usermapper.updatepassword(md5,username);
    }

    @Autowired
    private Usermapper usermapper;
    @Override
    public User Findbyusername(String username) {
        return usermapper.Findbyusername(username);
    }

    @Override
    public void adduser(String username, String userpassword) {
        String md5 = MD5Util.getMD5(userpassword);

        usermapper.adduser(username,md5);
    }
    @Override
    public boolean login(String username) {
        User user = usermapper.Findbyusername(username);
        if(user==null){
            return false;
        }else {
            return true;
        }
    }
}

